package com.kayt.cyberBar.cyber.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cyber.business.dao.ICyberExtraFinanceDao;
import com.kayt.cyberBar.cyber.business.entity.po.BCyberExtraFinance;

@Repository
public class CyberExtraFinanceDaoImpl implements ICyberExtraFinanceDao {

	@Autowired
	private EntityDao entityDao;
	
	@Override
	public Long addFinance (BCyberExtraFinance finance) throws SQLException {
		return this.entityDao.save(finance);
	}
	
	@Override
	public void batchInsert (List<BCyberExtraFinance> itemList) {
		this.entityDao.batchInsert(itemList);
	}
	
	@Override
	public void updateFinance (BCyberExtraFinance finance) throws SQLException {
		this.entityDao.update(finance);
	}
	
	@Override
	public int deleteFinance (BCyberExtraFinance finance) {
		if (finance != null) {
			this.entityDao.deleteDetached(finance);
			return 0;
		}
		return -1;
	}
	
	@Override
	public BCyberExtraFinance getFinanceById (String financeId) {
		BCyberExtraFinance finance = null;
		if (financeId != null) {
			Long id;
			try {
				id = Long.parseLong(financeId);
			} catch (Exception e) {
				return null;
			}
			finance = this.entityDao.findById(id, BCyberExtraFinance.class);
		}
		return finance;
	}
	
	@Override
	public BCyberExtraFinance getFinanceByCode (String financeCode) throws SQLException {
		BCyberExtraFinance finance = null;
		if (financeCode != null) {
			StringBuffer hql = new StringBuffer(" o.code=?");
			finance = this.entityDao.getObj(BCyberExtraFinance.class, hql.toString(), new Object[]{financeCode});
		}
		return finance;
	}
	
	@Override
	public PageData<BCyberExtraFinance> getFinanceList (int pageNun, int pageSize, String hql, Object[] params, LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<BCyberExtraFinance> salePageData = this.entityDao.getPagingData(BCyberExtraFinance.class, pageNun, pageSize, hql, params, orderMap);
		return salePageData;
	}
	
	@Override
	public List getFinanceListByHql(int pageNum, int pageSize, String hql, Object[] params) throws SQLException {
		return this.entityDao.getPagingDataByHql(pageNum, pageSize, hql, params);
	}
	
	@Override
	public boolean checkValidate(String name) throws SQLException {
		if (StringUtils.isNotBlank(name)) {
			String hql = "select count(o.id) as count from BCyberExtraFinance o where o.name=? ";
	        List countListMap = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[]{name});
	        if(countListMap != null && !countListMap.isEmpty()) {
	            Map countMap = (Map)countListMap.get(0);
	            Long tempCount = (Long)countMap.get("count");
	            if (tempCount == 0) {
	            	return true;
	            }
	        }
		}
		return false;
	}
}
